package edu.iss.demo.web.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;





import edu.iss.demo.exception.LeaveTypeNotFound;
import edu.iss.demo.exception.StudentNotFound;
import edu.iss.demo.model.LeaveType;
import edu.iss.demo.model.Student;
import edu.iss.demo.service.LeaveTypeService;

@Controller
@RequestMapping(value = "/LeaveType")
public class LeaveTypeController {
	
	@Autowired
	private LeaveTypeService leaveTypeService;
	
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public ModelAndView newLeaveTypepage() {
		ModelAndView mav = new ModelAndView("LeaveType-new", "LeaveType",new LeaveType());
		return mav;
	}

	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public ModelAndView createNewLeaveType(@ModelAttribute @Valid LeaveType leaveType,
			BindingResult result, final RedirectAttributes redirectAttributes) {

		if (result.hasErrors())
			return new ModelAndView("LeaveType-new");

		leaveTypeService.create(leaveType);
		
		ModelAndView mav = new ModelAndView();
		String message = "New Leave Type " + leaveType.getName()
				+ " was successfully created.";

		mav.setViewName("AdminHome");

		redirectAttributes.addFlashAttribute("message", message);
		return mav;
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView leavetypeListPage() {
		ModelAndView mav = new ModelAndView("leavetype-list");
		List<LeaveType> leavetypeList = leaveTypeService.findAll();
		mav.addObject("leaveTypelist", leavetypeList);
		return mav;
	}
	
	
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public ModelAndView editLeaveType(@PathVariable Long id) {
		ModelAndView mav = new ModelAndView("leavetype-edit");
		LeaveType leaveType = leaveTypeService.findById(id);
		mav.addObject("LeaveType", leaveType);
		System.out.println("leave type id--->"+leaveType.getLeaveTypeId());
		return mav;
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
	public ModelAndView editLeaveType(@ModelAttribute @Valid LeaveType leaveType,
			BindingResult result, @PathVariable Long id,
			final RedirectAttributes redirectAttributes) throws LeaveTypeNotFound {
		
		leaveType.setLeaveTypeId(id);
		
		if (result.hasErrors())
			return new ModelAndView("leavetype-edit");

		ModelAndView mav = new ModelAndView("AdminHome");
		String message = "Leave Type was successfully updated.";

		leaveTypeService.update(leaveType);

		redirectAttributes.addFlashAttribute("message", message);
		return mav;
	}
	
	
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	public ModelAndView deleteLeaveType(@PathVariable Integer id,
			final RedirectAttributes redirectAttributes) throws LeaveTypeNotFound {

		ModelAndView mav = new ModelAndView("AdminHome");

		LeaveType leaveType = leaveTypeService.delete(id);
		String message = "The leave type " + leaveType.getName()
				+ " was successfully deleted.";

		redirectAttributes.addFlashAttribute("message", message);
		return mav;
	}

	
	
	

}
